<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件委托</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <style>
       ul{
           background:blue;
           height:400px;
       }
    </style>
</head>
<body>
 
    <ul id="box">
        <li>li1</li>
        <li>li2</li>
        <li>li3</li>
        <li>li4</li>
        <li>li5</li>
    </ul>
    <script>
    //   事件委拖
        var box = document.getElementById("box");
        box.addEventListener("click",function(event){
            console.log(event.target.localName)
            var target = event.target;
            //防止父元素ul也触发事件
            if(target.nodeName == "LI"){
                target.style.backgroundColor = "red";
            }
        })

      //普通事件
        var box = document.getElementById("box");
        var li = box.getElementsByTagName("li");
        for(var i=0;i<li.length;i++){
            li[i].addEventListener('click',function(){
                this.style.backgroundColor='red'
           })
        }
    </script>
</body>
</html>